package com.mm.serivce.quartz.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.domain.common.DCornQuartz;
import com.mm.mapper.note.DCornQuartzMapper;
import com.mm.serivce.quartz.CornQuartzService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 描述: 定时器信息实现
 *
 * @author: yanglin
 * @Date: 2021-07-29-8:12
 * @Version: 1.0
 */
@Service("cornQuartzService")
public class CornQuartzServiceImpl extends ServiceImpl<DCornQuartzMapper, DCornQuartz> implements CornQuartzService {

    private final DCornQuartzMapper quartzMapper;

    public CornQuartzServiceImpl(DCornQuartzMapper quartzMapper) {
        this.quartzMapper = quartzMapper;
    }

    @Override
    public DCornQuartz queryQuartzById(Long taskId) {
        return quartzMapper.selectById(taskId);
    }

    @Override
    public int updateQuartzById(DCornQuartz quartz) {
        return quartzMapper.updateById(quartz);
    }

    @Override
    public List<DCornQuartz> queryQuartzList(String day) {
        // 格式化时间 07-31;失败则查询当天
        LambdaQueryWrapper<DCornQuartz> queryWrapper = new LambdaQueryWrapper();
        Date today = new Date();
        Date start = DateUtil.beginOfMonth(today);
        Date end = DateUtil.endOfMonth(today);
        if (StrUtil.isNotBlank(day) && day.contains("-")) {
            today = DateUtil.parse(day, "yyyy-MM-dd");
            start = DateUtil.beginOfDay(today);
            end = DateUtil.endOfDay(today);
        }
        queryWrapper.ge(DCornQuartz::getCreateTime, start);
        queryWrapper.le(DCornQuartz::getCreateTime, end);
        queryWrapper.eq(DCornQuartz::getDelFlg, '0');
        return quartzMapper.selectList(queryWrapper);
    }

    @Override
    public List<DCornQuartz> queryQuartzList(Map<String, Object> map) {
        String day = map.get("day").toString();
        String jobName = map.get("jobName").toString();
        // 格式化时间 07-31;失败则查询当天
        LambdaQueryWrapper<DCornQuartz> queryWrapper = new LambdaQueryWrapper();
        Date today = new Date();
        Date start = DateUtil.beginOfMonth(today);
        Date end = DateUtil.endOfMonth(today);
        if (StrUtil.isNotBlank(day) && day.contains("-")) {
            today = DateUtil.parse(day, "yyyy-MM-dd");
            start = DateUtil.beginOfDay(today);
            end = DateUtil.endOfDay(today);
        }
        if (StrUtil.isNotBlank(jobName)) {
            queryWrapper.like(DCornQuartz::getJobName, jobName);
        }
        queryWrapper.ge(DCornQuartz::getCreateTime, start);
        queryWrapper.le(DCornQuartz::getCreateTime, end);
        queryWrapper.eq(DCornQuartz::getDelFlg, '0');
        return quartzMapper.selectList(queryWrapper);
    }

    @Override
    public boolean remove(Long nId) {
        boolean flag = false;
        DCornQuartz quartz = quartzMapper.selectById(nId);
        if (Objects.nonNull(quartz)) {
            quartz.setDelFlg(1);
            Integer result = quartzMapper.updateById(quartz);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }

}
